/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */
import { BaseEvent, EventAdditional, EventBody, EventHead, MetricEventType } from './base-event.js';

export enum MarkEventType {
  HISTORY = 'history',
  OTHER = 'other',
}

export enum MarkEventCategory {
  BUILD = 'build',
  CLEAN = 'clean',
}

export enum MarkEventState {
  SUCCESS = 'success',
  FAILED = 'failed',
  RUNNING = 'running',
}

export class MarkEventTime {
  year: number;
  month: number;
  day: number;
  hour: number;
  minute: number;
  second: number;

  constructor(time: Date) {
    this.year = time.getFullYear();
    this.month = time.getMonth() + 1;
    this.day = time.getDate();
    this.hour = time.getHours();
    this.minute = time.getMinutes();
    this.second = time.getSeconds();
  }
}

export class MarkEventAdditional implements EventAdditional {
  markType?: MarkEventType;
  category?: MarkEventCategory;
  state?: MarkEventState;
  time: MarkEventTime;
  hvigorVersion?: string;
  completeCommand?: string;
  nodeVersion?: string;

  constructor() {
    this.time = new MarkEventTime(new Date());
  }
}

export class MarkEvent extends BaseEvent {
  additional: MarkEventAdditional;

  constructor(id: string, name: string, description: string, pid: number, tid: string) {
    super(new EventHead(id, name, description, MetricEventType.MARK), new EventBody(pid, tid));
    this.additional = new MarkEventAdditional();
  }

  start(state = MarkEventState.RUNNING, time?: number) {
    this.setState(state);
    super.setStartTime(time);
  }

  stop(state = MarkEventState.SUCCESS, time?: number) {
    if (this.additional.state === MarkEventState.FAILED || this.additional.state === MarkEventState.SUCCESS) {
      return;
    }
    this.body.endTime = time ?? Number(process.hrtime.bigint());

    this.setState(state);
  }

  setMarkType(markType: MarkEventType) {
    this.additional.markType = markType;
  }

  setCategory(category: MarkEventCategory) {
    this.additional.category = category;
  }

  setState(state: MarkEventState) {
    this.additional.state = state;
  }

  setHvigorVersion(hvigorVersion: string) {
    this.additional.hvigorVersion = hvigorVersion;
  }

  setCompleteCommand(completeCommand: string) {
    this.additional.completeCommand = completeCommand;
  }

  setNodeVersion(nodeVersion: string) {
    this.additional.nodeVersion = nodeVersion;
  }
}
